package com.bdqn.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bdqn.bus.dao.CarMapper;
import com.bdqn.bus.entity.Car;
import com.bdqn.bus.entity.Rent;
import com.bdqn.bus.dao.RentMapper;
import com.bdqn.bus.service.RentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.common.utitls.SystemConstants;
import com.bdqn.common.utitls.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author KazuGin
 * @since 2020-05-14
 */
@Service
@Transactional
public class RentServiceImpl extends ServiceImpl<RentMapper, Rent> implements RentService {

    @Resource
    private CarMapper carMapper;

    @Override
    public boolean save(Rent entity) {
        //出租单号 随机数生成
        entity.setRentid(UUIDUtil.createRandomStringUseTime("CZ"));
        //创建时间
        entity.setCreatetime(new Date());
        //出租状态
        entity.setRentflag(SystemConstants.RENT_FLAG_FALSE);
        //调用保存出租单信息方法
        if (super.save(entity)){
            //修改汽车出租的状态
            Car car=new Car();
            car.setIsrenting(SystemConstants.ISRENTING_TRUE);//出租的状态
            //创建条件构造器
            UpdateWrapper<Car> updateWrapper=new UpdateWrapper<Car>();
            updateWrapper.eq("carnumber",entity.getCarnumber());
            //调用修改汽车信息的方法
            carMapper.update(car,updateWrapper);
            return  true;//添加成功
        }
        return false; //失败
    }
}
